<template>
	<el-container>

		<el-aside width="300px" v-loading="menuloading">
			<el-container>
				<el-header>
					<el-input placeholder="输入关键字进行过滤" v-model="menuFilterText" clearable></el-input>
				</el-header>
				<el-main class="nopadding">
					<el-tree show-checkbox ref="menu" class="menu" node-key="id" :data="menuList" :props="menuProps" draggable highlight-current :expand-on-click-node="false" check-strictly  :filter-node-method="menuFilterNode" @node-click="menuClick" @node-drop="nodeDrop">
						<template #default="{node, data}">

							<span class="custom-tree-node el-tree-node__label">
								<span class="label">
									{{ node.label }}
								</span>
								<span class="do">
									<el-icon @click.stop="add(node, data)"><el-icon-plus /></el-icon>
								</span>
							</span>
						</template>
					</el-tree>
				</el-main>
				<el-footer style="height:51px;">
					<el-button type="primary" size="small" icon="el-icon-plus" @click="add()"></el-button>
					<el-button type="danger" size="small" plain icon="el-icon-delete" @click="delMenu"></el-button>
				</el-footer>
			</el-container>
		</el-aside>
		<el-container>
			<el-main class="nopadding" style="padding:20px;" ref="main">
				<save ref="save" :menu="menuList"></save>
			</el-main>
		</el-container>
	</el-container>
</template>

<script>
	import save from './save'

	export default {
		name: "power",
		components: {
			save
		},
		data(){
			return {
				menuloading: false,
				menuList: [],
				menuProps: {
					label: (data)=>{
						return data.meta.title
					}
				},
				index: 10000,
				menuFilterText: ""
			}
		},
		watch: {
			menuFilterText(val){
				this.$refs.menu.filter(val);
			}
		},
		mounted() {
			this.getMenu();
		},
		methods: {
			//加载树数据
			async getMenu(){
				this.menuloading = true
				var res = await this.$API.basic.power.list.post({data:{}});
				if (res.code==0){
					this.menuloading = false
					this.menuList = res.data.menu
				}
			},
			//树点击
			menuClick(data){
				this.$refs.save.setData(data, data.pid)
				this.$refs.main.$el.scrollTop = 0
			},
			//树过滤
			menuFilterNode(value, data){
				if (!value) return true;
				var targetText = data.name;
				return targetText.indexOf(value) !== -1;
			},
            //树拖拽
            async nodeDrop(draggingNode, dropNode, dropType){
                this.menuloading = true
                if (dropType == 'before' || dropType == 'after') {
                    // 拖拽对象的pid=释放对象的pid
                    draggingNode.data.pid = dropNode.data.pid

                } else if (dropType == 'inner') {
                    // 拖拽对象的pid=释放对象的id
                    draggingNode.data.pid = dropNode.data.id

                }
                var res = await this.$API.basic.power.edit.post({data:draggingNode.data})
                if(res.code == 0){
                    this.$message.success("保存成功")
                }
                this.menuloading = false
            },
			//增加
			async add(node, data){
			    this.index++;
                var newMenuData = {
                  //  id: this.index,
                    pid: data ? data.id : 0,
                    type: data ? data.type : 0,
					meta: {
                        title: '未命名',
						icon: "",
						affix:false
					}
                }
                this.$refs.menu.append(newMenuData, node)
                this.$refs.menu.setCurrentKey(newMenuData.id)
                this.$refs.save.setData(newMenuData, newMenuData.pid)
			},
            //删除菜单
            async delMenu(){
                var checkedNodes = this.$refs.menu.getCheckedNodes()
                if(checkedNodes.length == 0){
                    this.$message.warning("请选择需要删除的项")
                    return false;
                }

                var confirm = await this.$confirm('确认删除已选择的菜单吗？','提示', {
                    type: 'warning',
                    confirmButtonText: '删除',
                    confirmButtonClass: 'el-button--danger'
                }).catch(() => {})
                if(confirm != 'confirm'){
                    return false
                }

                this.menuloading = true
                var ids = []
                checkedNodes.map(item => ids.push(item.id))
                var reqData = {
                    powerIds: ids
                }
                var res = await this.$API.basic.power.delete.post({data:reqData});
                if (res && res.code == 0){
                    this.getMenu()
                    this.$message.success('删除成功')
                    checkedNodes.forEach(item => {
                        var node = this.$refs.menu.getNode(item)
                        if(node && node.isCurrent){
                            this.$refs.save.setData({})
                        }
                        this.$refs.menu.remove(item)
                    })
                }
                this.menuloading = false
                return false;
            }
		}
	}
</script>

<style scoped>
	.custom-tree-node {display: flex;flex: 1;align-items: center;justify-content: space-between;font-size: 14px;padding-right: 24px;height:100%;}
	.custom-tree-node .label {display: flex;align-items: center;;height: 100%;}
	.custom-tree-node .label .el-tag {margin-left: 5px;}
	.custom-tree-node .do {display: none;}
	.custom-tree-node .do i {margin-left:5px;color: #999;}
	.custom-tree-node .do i:hover {color: #333;}

	.custom-tree-node:hover .do {display: inline-block;}
</style>
